<?php
namespace Home\Controller;

use Think\Controller;
class ReportController extends BaseController{

	//用户举报
	public function do_report(){
		$key = array('report_user_id','reported_user_id','type','report_content');
		$return_data = array();
		$data = array();

       foreach ($key as $dest => $value) {
           if(!$_POST[''.$value]){
           		//当举报用户未授权登录，即没拿到user_id时
	            if($value=='report_user_id'){
		            $return_data['error_code'] = 2;
		            $return_data['msg'] = '用户未登录(缺少report_user_id)';
		            $this->ajaxReturn($return_data);
	            }

	            $return_data['error_code'] = 1;
	            $return_data['msg'] = '参数不足:'.$value;
	            $this->ajaxReturn($return_data);
           }else{
           		$data[''.$value] = $_POST[''.$value];
           }


       }

       $reportlist = M('report_list');
       $qeslist = M('qes_list');
       $filelist = M('file_info'); 
       $where = array();

        if(!$_POST['xx_id'] || !$_POST['xx_content']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:xx_id or xx_content';
            $this->ajaxReturn($return_data);
        }
       
       if($_POST['type'] == 1){                                  //举报问题

	        $where['qes_id'] = $_POST['xx_id'];
	        $result = $qeslist->where($where)->find();
	        if(!$result){
	        	$return_data['error_code'] = 3;
	            $return_data['msg'] = '该问题不存在';
	            $this->ajaxReturn($return_data);
	        }else{
	        	$data['qes_id'] = $_POST['xx_id'];
	        	$data['qes_content'] = $_POST['xx_content'];

	        	//添加举报信息的结果
	        	$result2 = $reportlist->add($data);    //返回该条数据的id
	        }
       }else if($_POST['type'] == 2){                           //举报文件


	        $where['file_id'] = $_POST['xx_id'];
	        $result = $qeslist->where($where)->find();
	        if(!$result){
	        	$return_data['error_code'] = 3;
	            $return_data['msg'] = '该文件不存在';
	            $this->ajaxReturn($return_data);
	        }else{
	        	$data['file_id'] = $_POST['xx_id'];
	        	$data['file_name'] = $_POST['xx_content'];

	        	//添加举报信息的结果
	        	$result2 = $reportlist->add($data);    //返回该条数据的id
	        }
       }else if($_POST['type'] == 3){                           //举报文件

	        $where['id'] = $_POST['xx_id'];
	        $result = $qeslist->where($where)->find();
	        if(!$result){
	        	$return_data['error_code'] = 3;
	            $return_data['msg'] = '该评论不存在';
	            $this->ajaxReturn($return_data);
	        }else{
	        	$data['comment_id'] = $_POST['xx_id'];
	        	$data['comment_content'] = $_POST['xx_content'];

	        	//添加举报信息的结果
	        	$result2 = $reportlist->add($data);    //返回该条数据的id
	        }
       }

       //添加后返回信息
       if(!$result2){
   			$return_data['error_code'] = 4;
            $return_data['msg'] = '数据添加失败';
            $this->ajaxReturn($return_data);
       }else{
   			$return_data['error_code'] = 0;
            $return_data['msg'] = '数据添加成功';
            $return_data['data'] = $data;
            $return_data['data']['report_id'] = $result2;
            $this->ajaxReturn($return_data);
       }

	}

	//用户反馈
	public function feedback(){
		$key = array('user_id','content');
		$return_data = array();
		$data = array();

       foreach ($key as $dest => $value) {
           if(!$_POST[''.$value]){
           		//当举报用户未授权登录，即没拿到user_id时
	            if($value=='report_user_id'){
		            $return_data['error_code'] = 2;
		            $return_data['msg'] = '用户未登录(却少user_id)';
		            $this->ajaxReturn($return_data);
	            }

	            $return_data['error_code'] = 1;
	            $return_data['msg'] = '参数不足:'.$value;
	            $this->ajaxReturn($return_data);
           }else{
           		$data[''.$value] = $_POST[''.$value];
           }
       }

       $feedback = M('feedback');
       $result = $feedback->add($data);

       if($result){
   			$return_data['error_code'] = 0;
            $return_data['msg'] = '数据添加成功';
            $this->ajaxReturn($return_data);
   		}else{
			$return_data['error_code'] = 3;
            $return_data['msg'] = '数据添加失败';
            $this->ajaxReturn($return_data);
   		}
	}

	//查看，后台
    public function read_one(){
	    $return_data=array();

        if(!$_POST['id']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：id';
            $this->ajaxReturn($return_data);
        }
        $reportlist=M('report_list');
        $qeslist=M('qes_list');
        $fileinfo=M('file_info');
        $commentinfo=M('comment_info');

        $where=array();
        $where['report_id']=$_POST['id'];
        $result=$reportlist->where($where)->find();

        if($result){
            if($result['type']==1){
                $where=array();
                $where['qes_id']=$result['qes_id'];
                $result2=$qeslist->where($where)->find();
            }else if($result['type']==2){
                $where=array();
                $where['file_id']=$result['file_id'];
                $result2=$fileinfo->where($where)->find();
            }else{
                $where=array();
                $where['id']=$result['comment_id'];
                $result2=$commentinfo->where($where)->find();
            }
            $return_data['error_code'] = 0;
            $return_data['report']=$result;
            $return_data['data']=$result2;
            $this->ajaxReturn($return_data);
        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '该举报记录不存在';
            $this->ajaxReturn($return_data);
        }
    }

	//举报，后台
    public function report_sec(){
	    if(!$_POST['id'] || !$_POST['secor']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：id or secor';
            $this->ajaxReturn($return_data);
        }
	    $return_data=array();

	    $reportlist = M('report_list');
        $my_message=M('my_message');
        $qeslist=M('qes_list');

	    $where=array();
	    $where['report_id']=$_POST['id'];
	    $result=$reportlist->where($where)->find();

	    $data=array();

	    //找到举报记录
	    if($result){
	        $data['my_id']=$result['reported_user_id'];
            $data['report_type']=$result['type'];

	        if($_POST['secor']==0){
                $result=$reportlist->where($where)->delete();
                $return_data['error_code'] = 0;
                $return_data['msg'] = '该举报记录删除成功';
                $this->ajaxReturn($return_data);
            }else if($_POST['secor']==1){

	            //删除问题，以及问题下的回答，消息
	            if($result['type']==1){
	                $data['report_content'] = $result['qes_content'];

                    $where2 = array();
                    $where2['qes_id'] = $result['qes_id'];

                    $qeslist = M('qes_list');
                    $result2 = $qeslist->where($where2)->delete();

                    if($result2){
                        //删除数据库
                        $my_message->where($where2)->delete();
                        $answer_qes=M('answer_qes');
                        $answer_qes->where($where2)->delete();
                        $comment_info=M('comment_info');
                        $comment_info->where($where2)->delete();

                    }else{
                        $return_data['error_code'] = 2;
                        $return_data['msg'] = '删除问题失败（可能是问题不存在）';
                    }

                }else if($result['type']==2){
                    $data['report_content'] = $result['file_name'];

                    $where2 = array();
                    $where2['file_id'] = $result['file_id'];

                    $fileinfo = M('file_info');
                    $result2 = $fileinfo->where($where2)->delete();

                    $my_message->where($where2)->delete();
                    //answer_qes表
                    $answer_qes=M('answer_qes');
                    $result3=$answer_qes->where($where2)->find();
                    if($result3){
                        //改变问题的评论数
                        $condition=array();
                        $condition['qes_id']=$result3['qes_id'];
                        $qesone=$qeslist->where($condition)->find();

                        $data2=array();
                        $data2['comment_num']=$qesone['comment_num']-1;
                        $qeslist->where($condition)->save($data2);

                        $answer_qes->where($where2)->delete();
                    }

                    if(!$result2){
                        $return_data['error_code'] = 2;
                        $return_data['msg'] = '删除文件失败（可能是文件不存在）';
                    }

                }else{
                    $data['report_content'] = $result['comment_content'];

                    $where2 = array();
                    $where2['id'] = $result['comment_id'];
                    $comment_info = M('comment_info');
                    $result2 = $comment_info->where($where2)->delete();

                    $where2 = array();
                    $where2['comment_id'] = $result['comment_id'];
                    $my_message->where($where2)->delete();

                    $answer_qes=M('answer_qes');
                    $result3=$answer_qes->where($where2)->find();
                    if($result3){
                        //改变问题的评论数
                        $condition=array();
                        $condition['qes_id']=$result3['qes_id'];
                        $qesone=$qeslist->where($condition)->find();

                        $data2=array();
                        $data2['comment_num']=$qesone['comment_num']-1;
                        $qeslist->where($condition)->save($data2);

                        $answer_qes->where($where2)->delete();
                    }

                }
                //增加消息记录
                $my_message->add($data);
	            //删除举报记录
                $reportlist->where($where)->delete();

                $return_data['error_code'] = 0;
                $return_data['msg'] = '违规内容删除成功';
                $this->ajaxReturn($return_data);

            }
        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '该举报记录不存在';
            $this->ajaxReturn($return_data);
        }

    }

}